package com.fengpanlong.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.fengpanlong.entity.Employee;
import com.fengpanlong.util.DBHelp;
import com.fengpanlong.util.RowMapper;

public class EmployeeDao {
	private DBHelp<Employee> db = new DBHelp<Employee>();
	
	private class EmployeeRowMapper implements RowMapper<Employee> {

		public Employee mapperRow(ResultSet rs) throws SQLException {
			Employee e = new Employee();
			e.setId(rs.getString("id"));
			e.setUsername(rs.getString("username"));
			e.setPassword(rs.getString("password"));
			e.setPicture(rs.getString("picture"));
			return e;
		}
		
	}
	/**
	 * 查找所有用户
	 * @return 用户列表
	 */
	public List<Employee> findAll() {
		String sql = "select id,username,password,picture from t_employee";
		return db.executeQueryForList(sql, new EmployeeRowMapper());
	}
	/**
	 * 通过用户名查找用户
	 * @param managerName 用户名
	 * @return 用户对象
	 */
	public Employee findByName(String managerName) {
		String sql = "select id,username,password,picture from t_employee where username = ?";
		return db.executeQueryForObject(sql, new EmployeeRowMapper(), managerName);
	}
	/**
	 * 通过用户名列表查找用户列表
	 * @param managerName 用户名列表
	 * @return 用户列表
	 */
	public List<Employee> findByList(List<String> employeeNamesList) {
		List<Employee> employeeList = new ArrayList<Employee>();
		for(String username : employeeNamesList) {
			Employee e = findByName(username);
			if(e != null) {
				employeeList.add(e);
			}
		}
		return employeeList;
	}
	/**
	 * 通过id查找用户
	 * @param employeeId 用户id
	 * @return 用户对象
	 */
	public Employee findById(String employeeId) {
		String sql = "select id,username,password,picture from t_employee where id = ?";
		return db.executeQueryForObject(sql, new EmployeeRowMapper(), employeeId);
	}
	/**
	 * 通过任务id查找 任务人
	 * @param taskId 任务id
	 * @return 任务人
	 */
	public Employee findEmployeeByTaskId(String taskId) {
		String sql = "select id,username,password,picture from t_employee where id in (select employeeid from t_task where id = ?)";
		return db.executeQueryForObject(sql, new EmployeeRowMapper(), taskId);
	}
	/**
	 * 通过想法id查找联系人
	 * @param ideaId
	 * @return
	 */
	public Employee findByIdeaId(String ideaId) {
		String sql = "select id,username,password,picture from t_employee where id in (select employeeid from t_idea where id = ?)";
		return db.executeQueryForObject(sql, new EmployeeRowMapper(), ideaId);
	}
	/**
	 * 通过想法记录id查找联系人
	 * @param ideaCommentId
	 * @return
	 */
	public Employee findByIdeaCommentId(String ideaCommentId) {
		String sql = "select id,username,password,picture from t_employee where id in (select employeeid from t_ideacomment where id = ?)";
		return db.executeQueryForObject(sql, new EmployeeRowMapper(), ideaCommentId);
	}
}
